package com.lsp.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import butterknife.ButterKnife;
import com.lsp.base.view.titlebar.TitleBar;
import com.lsp.base.view.titlebar.TitleBarInterface;
import com.lsp.data.net.RetrofitCallManager;
import rx.Observable;
import rx.Subscriber;

/**
 * 1.添加空页面处理
 * 2.添加title统一处理
 *
 * @author com.lsp.ibxs
 * @created 16/11/8 14:01
 */
public abstract class BaseFragment extends Fragment
    implements View.OnClickListener, TitleBarInterface {

  protected String TAG = "";
  private View rootView;
  private RetrofitCallManager.RetrofitCallManagerImpl mRetrofitCallManager;
  private TitleBar titleBar;
  protected LinearLayout llContentContainer;
  protected LayoutInflater mLayoutInflater;

  @Override public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TAG = this.getClass().getSimpleName();
    mLayoutInflater = LayoutInflater.from(getActivity());
  }

  @Nullable @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
      @Nullable Bundle savedInstanceState) {
    initVariable();
    if (getLayoutId() > 0) {
      rootView = inflater.inflate(R.layout.fragment_layout, null);
      initRootView(rootView);
      ButterKnife.bind(this, rootView);
      initView();
      return rootView;
    } else {
      rootView = super.onCreateView(inflater, container, savedInstanceState);
      ButterKnife.bind(this, rootView);
      return rootView;
    }
  }

  private void initRootView(View rootView) {
    titleBar = (TitleBar) rootView.findViewById(R.id.title_bar);
    llContentContainer = (LinearLayout) rootView.findViewById(R.id.ll_content_container);
    if (!initTitleBar()) {
      titleBar.hiddenTitleBar();
    }
    if (getLayoutId() > 0) {
      View contentView = mLayoutInflater.inflate(getLayoutId(), null);
      llContentContainer.addView(contentView, LinearLayout.LayoutParams.MATCH_PARENT,
          LinearLayout.LayoutParams.MATCH_PARENT);
    }
    initEmptyLayout();
  }

  protected void initEmptyLayout() {
    //仅供BaseEmptyFragment使用
  }

  /**
   * 是否显示titlebar   默认不显示  只有重写该方法  并设置titlebar 才显示
   */
  protected boolean initTitleBar() {

    return false;
  }

  /**
   * 初始化变量
   */
  protected abstract void initVariable();

  @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mRetrofitCallManager = new RetrofitCallManager.RetrofitCallManagerImpl();
    initData();
  }

  public <T> void manageRetrofitCall(Observable<T> observable, Subscriber<T> subscriber) {
    if (null != mRetrofitCallManager) {
      mRetrofitCallManager.manageRetrofitCall(observable, subscriber);
    }
  }

  protected int getEmptyId() {
    return 0;
  }

  protected int getLayoutId() {
    return 0;
  }

  /**
   * 初始化view
   */
  protected abstract void initView();

  /**
   * 初始化数据
   */
  protected abstract void initData();

  @Override public void onDestroyView() {
    super.onDestroyView();
    if (null != mRetrofitCallManager) {
      mRetrofitCallManager.unSubscribeAll();
      mRetrofitCallManager = null;
    }
  }

  @Override public void setTitleLeft(int imgRes, View.OnClickListener mOnClickListener) {
    if (null != titleBar) {
      titleBar.setTitleLeft(imgRes, mOnClickListener);
    }
  }

  @Override public void setTitleCenter(String text, View.OnClickListener mOnClickListener) {

    if (null != titleBar) {
      titleBar.setTitleCenter(text, mOnClickListener);
    }
  }

  @Override
  public void setTitleRight(String text, int imgRes, View.OnClickListener mOnClickListener) {

    if (null != titleBar) {
      titleBar.setTitleRight(text, imgRes, mOnClickListener);
    }
  }
}
